import random
import pyperclip
from pymouse import *
from pykeyboard import *
import time

m = PyMouse()
k = PyKeyboard()

midpoint_x = 960
start_x = 740
start_y = 142

# 1
start_coordinate = [midpoint_x, 718]
# 2
input_code_coordinate = [midpoint_x, 512]
# 3
input_affirmance_coordinate = [900, 577]
# 4
code_enter_coordinate = [midpoint_x, 805]
code_in_list = []
id = 0

# 读取文件
ttt = open('code.txt')
t1 = ttt.readlines()
for x in t1:
    xl = x.replace('\n', '').split('2022')
    for y in xl:
        if y != '':
            code_in_list.append('2022' + y)


def copy_code():
    global id
    s = code_in_list[id]
    id += 1
    print(s)
    pyperclip.copy(s)
    keyboard = PyKeyboard()
    keyboard.press_key(keyboard.control_key)  # 按下Ctrl键
    keyboard.tap_key('V')  # 点击V键
    time.sleep(1)
    keyboard.release_key(keyboard.control_key)  # 松开Ctrl键


def mouse_move_l_click(coordinate_in, delay):
    m.click(coordinate_in[0], coordinate_in[1], 1, 1)
    time.sleep(delay)


def move_bg(coordinate_in, delay):
    m.press(coordinate_in[0][0], coordinate_in[0][1], 1)
    time.sleep(delay)
    m.move(coordinate_in[1][0], coordinate_in[1][1])
    m.release(coordinate_in[1][0], coordinate_in[1][1], 1)
    time.sleep(delay)


def input_one_code():
    mouse_move_l_click(start_coordinate, 0.03)
    mouse_move_l_click(input_code_coordinate, 0.03)
    copy_code()
    time.sleep(0.03)
    mouse_move_l_click(input_affirmance_coordinate, 0.03)
    mouse_move_l_click(code_enter_coordinate, 0.03)


def change_window(num):
    '''
    切换窗口
    :param num:  窗口排序号 
    :return:  null
    '''
    in_str = str(num)
    k.press_keys([k.windows_l_key, in_str])
    k.release_key(k.windows_l_key)
    k.release_key(in_str)
    time.sleep(0.2)


start_fire = [midpoint_x, start_y + 680]
hero_fire_coordinate = [[midpoint_x, 800], [start_x + 300, start_y + 450]]
lvl_5_coordinate = [midpoint_x, 600]
lvl_5_change_coordinate = [midpoint_x, 600]


def hero_fire_5():
    mouse_move_l_click(start_fire, 4)
    for tim in range(0, 5):
        print('挑战次数：' + str(tim))
        for next_coordinate in hero_fire_coordinate:
            mouse_move_l_click(next_coordinate, 1)
        time.sleep(4)
    mouse_move_l_click(lvl_5_coordinate, 1)
    mouse_move_l_click(lvl_5_coordinate, 1)


def hero_fire():
    for ti in range(0, 20):
        hero_fire_5()
        print('挑战5次完成')


def code_input():
    totle = str(len(code_in_list))
    print('兑换次数', totle)
    times_num = 0
    while id < len(code_in_list):
        times_num += 1
        print('第{}次，{}/{}'.format(str(times_num), str(times_num), totle))
        input_one_code()
        time.sleep(5.1)


# 切换功能
def toggle_menu(cdts):
    print('切换功能')
    for cdt in cdts:
        if cdt['cls'] == 0:
            mouse_move_l_click(cdt['list'], 0.5)
        elif cdt['cls'] == 1:
            move_bg(cdt['list'], 0.5)


# 执行任务
def run_act(cdts):
    print('运行')
    for cdt in cdts:
        if cdt['cls'] == 0:
            mouse_move_l_click(cdt['list'], 7)
        elif cdt['cls'] == 1:
            move_bg(cdt['list'], 7)


gong_hui_list = [start_x + 410, start_y + 770]
home_list = [start_x + 45, start_y + 770]


def refresh_home():
    mouse_move_l_click(gong_hui_list, 1)
    time.sleep(0.5)
    mouse_move_l_click(home_list, 1)


def get_last_list(list):
    return list[len(list) - 1]


兑换 = [
    {'cls': 0, 'list': [420 + start_x, 130 + start_y]},
    {'cls': 1, 'list': [[420 + start_x, 100 + start_y], [start_x, 100 + start_y]]},
    {'cls': 0, 'list': [420 + start_x, 100 + start_y]},
    {'cls': 2, 'list': [427 + start_x, 778 + start_y]}
]

fire_end = {'cls': 0, 'list': [midpoint_x, 657 + start_y]}

# 0点 1滑 2推出
chMenu = {
    # 兑换切换
    '排位': [
        {'cls': 0, 'list': [400 + start_x, 400 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 550 + start_y]},
        {'cls': 2, 'list': [410 + start_x, 760 + start_y]}
    ],
    '巅峰': [
        {'cls': 0, 'list': [400 + start_x, 400 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        {'cls': 2, 'list': [410 + start_x, 760 + start_y]}
    ],
    '迷失': [
        {'cls': 1, 'list': [[100 + start_x, 20 + start_y], [start_x + 400, 20 + start_y]]},
        {'cls': 1, 'list': [[100 + start_x, 20 + start_y], [start_x + 400, 20 + start_y]]},
        {'cls': 1, 'list': [[100 + start_x, 20 + start_y], [start_x + 400, 20 + start_y]]},
        {'cls': 1, 'list': [[100 + start_x, 20 + start_y], [start_x + 400, 20 + start_y]]},
        {'cls': 0, 'list': [170 + start_x, 140 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 550 + start_y]},
        {'cls': 2, 'list': [410 + start_x, 770 + start_y]}
    ],
    '荣誉': [
        {'cls': 0, 'list': [400 + start_x, 400 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 660 + start_y]},
        {'cls': 2, 'list': [410 + start_x, 750 + start_y]}
    ]
}
act = {
    '排位': [
        # 0战斗 1跳过 2确定
        {'cls': 0, 'list': [midpoint_x, 600 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 2
        {'cls': 0, 'list': [midpoint_x, 600 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 3
        {'cls': 0, 'list': [midpoint_x, 600 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 4
        {'cls': 0, 'list': [midpoint_x, 600 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 5
        {'cls': 0, 'list': [midpoint_x, 600 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},

        {'cls': 0, 'list': [start_x + 180, 670 + start_y]},
    ],
    '巅峰': [
        # 1
        {'cls': 0, 'list': [start_x + 90, 450 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 2
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 3
        {'cls': 0, 'list': [start_x + 370, 450 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 4
        {'cls': 0, 'list': [start_x + 90, 450 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 5
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 6
        {'cls': 0, 'list': [start_x + 370, 450 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 7
        {'cls': 0, 'list': [start_x + 90, 450 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 8
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 9
        {'cls': 0, 'list': [start_x + 370, 450 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},
        # 10
        {'cls': 0, 'list': [start_x + 90, 450 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 450 + start_y]},

        # 领奖
        {'cls': 0, 'list': [start_x + 350, 150 + start_y]},
        {'cls': 0, 'list': [start_x + 300, 190 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 640 + start_y]},
        {'cls': 0, 'list': [start_x + 420, 670 + start_y]}
    ],
    '荣誉': [
        # 1
        {'cls': 0, 'list': [start_x + 350, 600 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        # 2
        {'cls': 0, 'list': [start_x + 350, 600 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        # 3
        {'cls': 0, 'list': [start_x + 350, 600 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        # 4
        {'cls': 0, 'list': [start_x + 350, 600 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        # 5
        {'cls': 0, 'list': [start_x + 350, 600 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        # 6
        {'cls': 0, 'list': [start_x + 350, 600 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        # 7
        {'cls': 0, 'list': [start_x + 350, 600 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        # 8
        {'cls': 0, 'list': [start_x + 350, 600 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        # 9
        {'cls': 0, 'list': [start_x + 350, 600 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 500 + start_y]},
        {'cls': 0, 'list': [midpoint_x, 670 + start_y]},

        {'cls': 2, 'list': [start_x + 410, 750 + start_y]},
    ],
    '迷失': [
        # 1
        {'cls': 0, 'list': [midpoint_x, 760 + start_y]},
        {'cls': 0, 'list': [start_x + 300, 470 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 680 + start_y]},
        # 2
        {'cls': 0, 'list': [midpoint_x, 760 + start_y]},
        {'cls': 0, 'list': [start_x + 300, 470 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 680 + start_y]},
        # 3
        {'cls': 0, 'list': [midpoint_x, 760 + start_y]},
        {'cls': 0, 'list': [start_x + 300, 470 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 680 + start_y]},
        # 4
        {'cls': 0, 'list': [midpoint_x, 760 + start_y]},
        {'cls': 0, 'list': [start_x + 300, 470 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 680 + start_y]},
        # 5
        {'cls': 0, 'list': [midpoint_x, 760 + start_y]},
        {'cls': 0, 'list': [start_x + 300, 470 + start_y]},
        fire_end,
        {'cls': 0, 'list': [midpoint_x, 680 + start_y]},
    ]
}


# mouse_move_l_click(chMenu["0"][0], 0.3)
# move_bg(chMenu["0"][1], 0.4)

def test_all_menu():
    refresh_home()
    for obj in chMenu:
        toggle_menu(chMenu[obj])
        time.sleep(0.4)
        run_act(act[obj])
        time.sleep(0.4)
        mouse_move_l_click(get_last_list(chMenu[obj])['list'], 0.5)
        time.sleep(0.4)
        refresh_home()
        time.sleep(4)


def test_one_menu(obj):
    toggle_menu(chMenu[obj])
    time.sleep(0.4)
    run_act(act[obj])
    time.sleep(0.4)
    mouse_move_l_click(get_last_list(chMenu[obj])['list'], 0.5)
    time.sleep(0.4)
    refresh_home()
    time.sleep(4)


# change_window(1)

ch = 10
if ch == 0:
    # 兑换码
    toggle_menu(兑换)
    code_input()
    mouse_move_l_click(get_last_list(兑换)['list'], 0.5)
elif ch == 1:
    # 英雄挑战
    hero_fire()

# test_all_menu()
# test_one_menu("迷失")

奖励 = [[start_x+40, start_y+244], [midpoint_x, start_y+636], [start_x+426,start_y+669]]

print(chMenu["迷失"])
print(act["迷失"])
print(奖励)
print(fire_end)
# xuhuang = [start_x+ 360, start_y + 380]
#
# for i in range(0, 6):
#     mouse_move_l_click(xuhuang, 0.3)
